<!DOCTYPE html>
<html lang="en-US">
<head>
    <meta charset="UTF-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="language" content="en" />
        <link href="./assets/ffd55088/css/bootstrap.css" rel="stylesheet">
<link href="./assets/5cf9384a/solarized_light.css" rel="stylesheet">
<link href="./assets/6c54116e/style.css" rel="stylesheet">
<script src="./assets/a44cef0f/jquery.js"></script>
<script src="./assets/ffd55088/js/bootstrap.js"></script>
<script src="./assets/8ac4e28a/jssearch.js"></script>    <title>Quick Start - RESTful Web Services - The Definitive Guide to Yii 2.0</title>
</head>
<body>

<div class="wrap">
    <nav id="w1080" class="navbar-inverse navbar-fixed-top navbar" role="navigation"><div class="navbar-header"><button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#w1080-collapse"><span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span></button><a class="navbar-brand" href="./index.html">The Definitive Guide to Yii 2.0</a></div><div id="w1080-collapse" class="collapse navbar-collapse"><ul id="w1081" class="navbar-nav nav"><li><a href="./index.html">Class reference</a></li>
<li class="dropdown"><a class="dropdown-toggle" href="#" data-toggle="dropdown">Extensions <b class="caret"></b></a><ul id="w1082" class="dropdown-menu"><li><a href="./ext-apidoc-index.html" tabindex="-1">apidoc</a></li>
<li><a href="./ext-authclient-index.html" tabindex="-1">authclient</a></li>
<li><a href="./ext-bootstrap-index.html" tabindex="-1">bootstrap</a></li>
<li><a href="./ext-codeception-index.html" tabindex="-1">codeception</a></li>
<li><a href="./ext-debug-index.html" tabindex="-1">debug</a></li>
<li><a href="./ext-elasticsearch-index.html" tabindex="-1">elasticsearch</a></li>
<li><a href="./ext-faker-index.html" tabindex="-1">faker</a></li>
<li><a href="./ext-gii-index.html" tabindex="-1">gii</a></li>
<li><a href="./ext-imagine-index.html" tabindex="-1">imagine</a></li>
<li><a href="./ext-jui-index.html" tabindex="-1">jui</a></li>
<li><a href="./ext-mongodb-index.html" tabindex="-1">mongodb</a></li>
<li><a href="./ext-redis-index.html" tabindex="-1">redis</a></li>
<li><a href="./ext-smarty-index.html" tabindex="-1">smarty</a></li>
<li><a href="./ext-sphinx-index.html" tabindex="-1">sphinx</a></li>
<li><a href="./ext-swiftmailer-index.html" tabindex="-1">swiftmailer</a></li>
<li><a href="./ext-twig-index.html" tabindex="-1">twig</a></li></ul></li>
<li><a href="./guide-README.html">Guide</a></li></ul><div class="navbar-form navbar-left" role="search">
  <div class="form-group">
    <input id="searchbox" type="text" class="form-control" placeholder="Search">
  </div>
</div>
</div></nav>
    <div id="search-resultbox" style="display: none;" class="modal-content">
        <ul id="search-results">
        </ul>
    </div>

    
<div class="row">
    <div class="col-md-2">
                <div id="navigation" class="list-group"><a class="list-group-item" href="#navigation-1064" data-toggle="collapse" data-parent="#navigation">Introduction <b class="caret"></b></a><div id="navigation-1064" class="submenu panel-collapse collapse"><a class="list-group-item" href="./guide-intro-yii.html">About Yii</a>
<a class="list-group-item" href="./guide-intro-upgrade-from-v1.html">Upgrading from Version 1.1</a></div>
<a class="list-group-item" href="#navigation-1065" data-toggle="collapse" data-parent="#navigation">Getting Started <b class="caret"></b></a><div id="navigation-1065" class="submenu panel-collapse collapse"><a class="list-group-item" href="./guide-start-installation.html">Installing Yii</a>
<a class="list-group-item" href="./guide-start-workflow.html">Running Applications</a>
<a class="list-group-item" href="./guide-start-hello.html">Saying Hello</a>
<a class="list-group-item" href="./guide-start-forms.html">Working with Forms</a>
<a class="list-group-item" href="./guide-start-databases.html">Working with Databases</a>
<a class="list-group-item" href="./guide-start-gii.html">Generating Code with Gii</a>
<a class="list-group-item" href="./guide-start-looking-ahead.html">Looking Ahead</a></div>
<a class="list-group-item" href="#navigation-1066" data-toggle="collapse" data-parent="#navigation">Application Structure <b class="caret"></b></a><div id="navigation-1066" class="submenu panel-collapse collapse"><a class="list-group-item" href="./guide-structure-overview.html">Overview</a>
<a class="list-group-item" href="./guide-structure-entry-scripts.html">Entry Scripts</a>
<a class="list-group-item" href="./guide-structure-applications.html">Applications</a>
<a class="list-group-item" href="./guide-structure-application-components.html">Application Components</a>
<a class="list-group-item" href="./guide-structure-controllers.html">Controllers</a>
<a class="list-group-item" href="./guide-structure-models.html">Models</a>
<a class="list-group-item" href="./guide-structure-views.html">Views</a>
<a class="list-group-item" href="./guide-structure-modules.html">Modules</a>
<a class="list-group-item" href="./guide-structure-filters.html">Filters</a>
<a class="list-group-item" href="./guide-structure-widgets.html">Widgets</a>
<a class="list-group-item" href="./guide-structure-assets.html">Assets</a>
<a class="list-group-item" href="./guide-structure-extensions.html">Extensions</a></div>
<a class="list-group-item" href="#navigation-1067" data-toggle="collapse" data-parent="#navigation">Handling Requests <b class="caret"></b></a><div id="navigation-1067" class="submenu panel-collapse collapse"><a class="list-group-item" href="./guide-runtime-overview.html">Overview</a>
<a class="list-group-item" href="./guide-runtime-bootstrapping.html">Bootstrapping</a>
<a class="list-group-item" href="./guide-runtime-routing.html">Routing and URL Creation</a>
<a class="list-group-item" href="./guide-runtime-requests.html">Requests</a>
<a class="list-group-item" href="./guide-runtime-responses.html">Responses</a>
<a class="list-group-item" href="./guide-runtime-sessions-cookies.html">Sessions and Cookies</a>
<a class="list-group-item" href="./guide-runtime-handling-errors.html">Handling Errors</a>
<a class="list-group-item" href="./guide-runtime-logging.html">Logging</a></div>
<a class="list-group-item" href="#navigation-1068" data-toggle="collapse" data-parent="#navigation">Key Concepts <b class="caret"></b></a><div id="navigation-1068" class="submenu panel-collapse collapse"><a class="list-group-item" href="./guide-concept-components.html">Components</a>
<a class="list-group-item" href="./guide-concept-properties.html">Properties</a>
<a class="list-group-item" href="./guide-concept-events.html">Events</a>
<a class="list-group-item" href="./guide-concept-behaviors.html">Behaviors</a>
<a class="list-group-item" href="./guide-concept-configurations.html">Configurations</a>
<a class="list-group-item" href="./guide-concept-aliases.html">Aliases</a>
<a class="list-group-item" href="./guide-concept-autoloading.html">Class Autoloading</a>
<a class="list-group-item" href="./guide-concept-service-locator.html">Service Locator</a>
<a class="list-group-item" href="./guide-concept-di-container.html">Dependency Injection Container</a></div>
<a class="list-group-item" href="#navigation-1069" data-toggle="collapse" data-parent="#navigation">Working with Databases <b class="caret"></b></a><div id="navigation-1069" class="submenu panel-collapse collapse"><a class="list-group-item" href="./guide-db-dao.html">Data Access Objects</a>
<a class="list-group-item" href="./guide-db-query-builder.html">Query Builder</a>
<a class="list-group-item" href="./guide-db-active-record.html">Active Record</a>
<a class="list-group-item" href="./guide-db-migrations.html">Migrations</a>
<a class="list-group-item" href="https://github.com/yiisoft/yii2-sphinx/blob/master/docs/guide/README.md">Sphinx</a>
<a class="list-group-item" href="https://github.com/yiisoft/yii2-redis/blob/master/docs/guide/README.md">Redis</a>
<a class="list-group-item" href="https://github.com/yiisoft/yii2-mongodb/blob/master/docs/guide/README.md">MongoDB</a>
<a class="list-group-item" href="https://github.com/yiisoft/yii2-elasticsearch/blob/master/docs/guide/README.md">ElasticSearch</a></div>
<a class="list-group-item" href="#navigation-1070" data-toggle="collapse" data-parent="#navigation">Getting Data from Users <b class="caret"></b></a><div id="navigation-1070" class="submenu panel-collapse collapse"><a class="list-group-item" href="./guide-input-forms.html">Creating Forms</a>
<a class="list-group-item" href="./guide-input-validation.html">Validating Input</a>
<a class="list-group-item" href="./guide-input-file-upload.html">Uploading Files</a>
<a class="list-group-item" href="./guide-input-tabular-input.html">Collecting Tabular Input</a>
<a class="list-group-item" href="./guide-input-multiple-models.html">Getting Data for Multiple Models</a></div>
<a class="list-group-item" href="#navigation-1071" data-toggle="collapse" data-parent="#navigation">Displaying Data <b class="caret"></b></a><div id="navigation-1071" class="submenu panel-collapse collapse"><a class="list-group-item" href="./guide-output-formatting.html">Data Formatting</a>
<a class="list-group-item" href="./guide-output-pagination.html">Pagination</a>
<a class="list-group-item" href="./guide-output-sorting.html">Sorting</a>
<a class="list-group-item" href="./guide-output-data-providers.html">Data Providers</a>
<a class="list-group-item" href="./guide-output-data-widgets.html">Data Widgets</a>
<a class="list-group-item" href="./guide-output-client-scripts.html">Working with Client Scripts</a>
<a class="list-group-item" href="./guide-output-theming.html">Theming</a></div>
<a class="list-group-item" href="#navigation-1072" data-toggle="collapse" data-parent="#navigation">Security <b class="caret"></b></a><div id="navigation-1072" class="submenu panel-collapse collapse"><a class="list-group-item" href="./guide-security-overview.html">Overview</a>
<a class="list-group-item" href="./guide-security-authentication.html">Authentication</a>
<a class="list-group-item" href="./guide-security-authorization.html">Authorization</a>
<a class="list-group-item" href="./guide-security-passwords.html">Working with Passwords</a>
<a class="list-group-item" href="./guide-security-cryptography.html">Cryptography</a>
<a class="list-group-item" href="./guide-structure-views.html#security">Views security</a>
<a class="list-group-item" href="https://github.com/yiisoft/yii2-authclient/blob/master/docs/guide/README.md">Auth Clients</a>
<a class="list-group-item" href="./guide-security-best-practices.html">Best Practices</a></div>
<a class="list-group-item" href="#navigation-1073" data-toggle="collapse" data-parent="#navigation">Caching <b class="caret"></b></a><div id="navigation-1073" class="submenu panel-collapse collapse"><a class="list-group-item" href="./guide-caching-overview.html">Overview</a>
<a class="list-group-item" href="./guide-caching-data.html">Data Caching</a>
<a class="list-group-item" href="./guide-caching-fragment.html">Fragment Caching</a>
<a class="list-group-item" href="./guide-caching-page.html">Page Caching</a>
<a class="list-group-item" href="./guide-caching-http.html">HTTP Caching</a></div>
<a class="list-group-item active" href="#navigation-1074" data-toggle="collapse" data-parent="#navigation">RESTful Web Services <b class="caret"></b></a><div id="navigation-1074" class="submenu panel-collapse collapse in"><a class="list-group-item active" href="./guide-rest-quick-start.html">Quick Start</a>
<a class="list-group-item" href="./guide-rest-resources.html">Resources</a>
<a class="list-group-item" href="./guide-rest-controllers.html">Controllers</a>
<a class="list-group-item" href="./guide-rest-routing.html">Routing</a>
<a class="list-group-item" href="./guide-rest-response-formatting.html">Response Formatting</a>
<a class="list-group-item" href="./guide-rest-authentication.html">Authentication</a>
<a class="list-group-item" href="./guide-rest-rate-limiting.html">Rate Limiting</a>
<a class="list-group-item" href="./guide-rest-versioning.html">Versioning</a>
<a class="list-group-item" href="./guide-rest-error-handling.html">Error Handling</a></div>
<a class="list-group-item" href="#navigation-1075" data-toggle="collapse" data-parent="#navigation">Development Tools <b class="caret"></b></a><div id="navigation-1075" class="submenu panel-collapse collapse"><a class="list-group-item" href="https://github.com/yiisoft/yii2-debug/blob/master/docs/guide/README.md">Debug Toolbar and Debugger</a>
<a class="list-group-item" href="https://github.com/yiisoft/yii2-gii/blob/master/docs/guide/README.md">Generating Code using Gii</a>
<a class="list-group-item" href="https://github.com/yiisoft/yii2-apidoc">Generating API Documentation</a></div>
<a class="list-group-item" href="#navigation-1076" data-toggle="collapse" data-parent="#navigation">Testing <b class="caret"></b></a><div id="navigation-1076" class="submenu panel-collapse collapse"><a class="list-group-item" href="./guide-test-overview.html">Overview</a>
<a class="list-group-item" href="./guide-test-environment-setup.html">Testing environment setup</a>
<a class="list-group-item" href="./guide-test-unit.html">Unit Tests</a>
<a class="list-group-item" href="./guide-test-functional.html">Functional Tests</a>
<a class="list-group-item" href="./guide-test-acceptance.html">Acceptance Tests</a>
<a class="list-group-item" href="./guide-test-fixtures.html">Fixtures</a></div>
<a class="list-group-item" href="#navigation-1077" data-toggle="collapse" data-parent="#navigation">Special Topics <b class="caret"></b></a><div id="navigation-1077" class="submenu panel-collapse collapse"><a class="list-group-item" href="https://github.com/yiisoft/yii2-app-advanced/blob/master/docs/guide/README.md">Advanced Project Template</a>
<a class="list-group-item" href="./guide-tutorial-start-from-scratch.html">Building Application from Scratch</a>
<a class="list-group-item" href="./guide-tutorial-console.html">Console Commands</a>
<a class="list-group-item" href="./guide-tutorial-core-validators.html">Core Validators</a>
<a class="list-group-item" href="./guide-tutorial-i18n.html">Internationalization</a>
<a class="list-group-item" href="./guide-tutorial-mailing.html">Mailing</a>
<a class="list-group-item" href="./guide-tutorial-performance-tuning.html">Performance Tuning</a>
<a class="list-group-item" href="./guide-tutorial-shared-hosting.html">Shared Hosting Environment</a>
<a class="list-group-item" href="./guide-tutorial-template-engines.html">Template Engines</a>
<a class="list-group-item" href="./guide-tutorial-yii-integration.html">Working with Third-Party Code</a></div>
<a class="list-group-item" href="#navigation-1078" data-toggle="collapse" data-parent="#navigation">Widgets <b class="caret"></b></a><div id="navigation-1078" class="submenu panel-collapse collapse"><a class="list-group-item" href="https://github.com/yiisoft/yii2-bootstrap/blob/master/docs/guide/README.md">Bootstrap Widgets</a>
<a class="list-group-item" href="https://github.com/yiisoft/yii2-jui/blob/master/docs/guide/README.md">jQuery UI Widgets</a></div>
<a class="list-group-item" href="#navigation-1079" data-toggle="collapse" data-parent="#navigation">Helpers <b class="caret"></b></a><div id="navigation-1079" class="submenu panel-collapse collapse"><a class="list-group-item" href="./guide-helper-overview.html">Overview</a>
<a class="list-group-item" href="./guide-helper-array.html">ArrayHelper</a>
<a class="list-group-item" href="./guide-helper-html.html">Html</a>
<a class="list-group-item" href="./guide-helper-url.html">Url</a></div></div>    </div>
    <div class="col-md-9 guide-content" role="main">
        <h1>Quick Start <span id="quick-start"></span><a href="#quick-start" class="hashlink">&para;</a></h1>
<div class="toc"><ol><li><a href="#creating-controller">Creating a Controller</a></li>
<li><a href="#configuring-url-rules">Configuring URL Rules</a></li>
<li><a href="#enabling-json-input">Enabling JSON Input</a></li>
<li><a href="#trying-it-out">Trying it Out</a></li>
<li><a href="#summary">Summary</a></li></ol></div>
<p>Yii provides a whole set of tools to simplify the task of implementing RESTful Web Service APIs.
In particular, Yii supports the following features about RESTful APIs:</p>
<ul>
<li>Quick prototyping with support for common APIs for <a href="guide-db-active-record.html">Active Record</a>;</li>
<li>Response format negotiation (supporting JSON and XML by default);</li>
<li>Customizable object serialization with support for selectable output fields;</li>
<li>Proper formatting of collection data and validation errors;</li>
<li>Support for <a href="http://en.wikipedia.org/wiki/HATEOAS">HATEOAS</a>;</li>
<li>Efficient routing with proper HTTP verb check;</li>
<li>Built-in support for the <code>OPTIONS</code> and <code>HEAD</code> verbs;</li>
<li>Authentication and authorization;</li>
<li>Data caching and HTTP caching;</li>
<li>Rate limiting;</li>
</ul>
<p>In the following, we use an example to illustrate how you can build a set of RESTful APIs with some minimal coding effort.</p>
<p>Assume you want to expose the user data via RESTful APIs. The user data are stored in the <code>user</code> DB table,
and you have already created the <a href="guide-db-active-record.html">active record</a> class <code>app\models\User</code> to access the user data.</p>
<h2>Creating a Controller  <span id="creating-controller"></span><a href="#creating-controller" class="hashlink">&para;</a></h2><p>First, create a <a href="guide-structure-controllers.html">controller</a> class <code>app\controllers\UserController</code> as follows,</p>
<pre><code class="hljs php language-php"><span class="hljs-keyword">namespace</span> <span class="hljs-title">app</span>\<span class="hljs-title">controllers</span>;

<span class="hljs-keyword">use</span> <span class="hljs-title">yii</span>\<span class="hljs-title">rest</span>\<span class="hljs-title">ActiveController</span>;

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">UserController</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">ActiveController</span>
</span>{
    <span class="hljs-keyword">public</span> <span class="hljs-variable">$modelClass</span> = <span class="hljs-string">'app\models\User'</span>;
}
</code></pre>
<p>The controller class extends from <a href="./yii-rest-activecontroller.html">yii\rest\ActiveController</a>, which implements a common set of RESTful actions.
By specifying <a href="./yii-rest-activecontroller.html#$modelClass-detail">modelClass</a>
as <code>app\models\User</code>, the controller knows which model can be used for fetching and manipulating data.</p>
<h2>Configuring URL Rules  <span id="configuring-url-rules"></span><a href="#configuring-url-rules" class="hashlink">&para;</a></h2><p>Then, modify the configuration about the <code>urlManager</code> component in your application configuration:</p>
<pre><code class="hljs php language-php"><span class="hljs-string">'urlManager'</span> =&gt; [
    <span class="hljs-string">'enablePrettyUrl'</span> =&gt; <span class="hljs-keyword">true</span>,
    <span class="hljs-string">'enableStrictParsing'</span> =&gt; <span class="hljs-keyword">true</span>,
    <span class="hljs-string">'showScriptName'</span> =&gt; <span class="hljs-keyword">false</span>,
    <span class="hljs-string">'rules'</span> =&gt; [
        [<span class="hljs-string">'class'</span> =&gt; <span class="hljs-string">'yii\rest\UrlRule'</span>, <span class="hljs-string">'controller'</span> =&gt; <span class="hljs-string">'user'</span>],
    ],
]
</code></pre>
<p>The above configuration mainly adds a URL rule for the <code>user</code> controller so that the user data
can be accessed and manipulated with pretty URLs and meaningful HTTP verbs.</p>
<h2>Enabling JSON Input  <span id="enabling-json-input"></span><a href="#enabling-json-input" class="hashlink">&para;</a></h2><p>To let the API accept input data in JSON format, configure the <a href="./yii-web-request.html#$parsers-detail">parsers</a> property of
the <code>request</code> <a href="guide-structure-application-components.html">application component</a> to use the <a href="./yii-web-jsonparser.html">yii\web\JsonParser</a> for JSON input:</p>
<pre><code class="hljs php language-php"><span class="hljs-string">'request'</span> =&gt; [
    <span class="hljs-string">'parsers'</span> =&gt; [
        <span class="hljs-string">'application/json'</span> =&gt; <span class="hljs-string">'yii\web\JsonParser'</span>,
    ]
]
</code></pre>
<blockquote class="info"><p><strong>Info: </strong>The above configuration is optional. Without the above configuration, the API would only recognize 
  <code>application/x-www-form-urlencoded</code> and <code>multipart/form-data</code> input formats.</p>
</blockquote>
<h2>Trying it Out  <span id="trying-it-out"></span><a href="#trying-it-out" class="hashlink">&para;</a></h2><p>With the above minimal amount of effort, you have already finished your task of creating the RESTful APIs
for accessing the user data. The APIs you have created include:</p>
<ul>
<li><code>GET /users</code>: list all users page by page;</li>
<li><code>HEAD /users</code>: show the overview information of user listing;</li>
<li><code>POST /users</code>: create a new user;</li>
<li><code>GET /users/123</code>: return the details of the user 123;</li>
<li><code>HEAD /users/123</code>: show the overview information of user 123;</li>
<li><code>PATCH /users/123</code> and <code>PUT /users/123</code>: update the user 123;</li>
<li><code>DELETE /users/123</code>: delete the user 123;</li>
<li><code>OPTIONS /users</code>: show the supported verbs regarding endpoint <code>/users</code>;</li>
<li><code>OPTIONS /users/123</code>: show the supported verbs regarding endpoint <code>/users/123</code>.</li>
</ul>
<blockquote class="info"><p><strong>Info: </strong>Yii will automatically pluralize controller names for use in endpoints.
You can configure this using the <a href="./yii-rest-urlrule.html#$pluralize-detail">yii\rest\UrlRule::$pluralize</a>-property.</p>
</blockquote>
<p>You may access your APIs with the <code>curl</code> command like the following,</p>
<pre><code class="hljs php">$ curl -i -H <span class="hljs-string">"Accept:application/json"</span> <span class="hljs-string">"http://localhost/users"</span>

HTTP/<span class="hljs-number">1.1</span> <span class="hljs-number">200</span> OK
...
X-Pagination-Total-Count: <span class="hljs-number">1000</span>
X-Pagination-Page-Count: <span class="hljs-number">50</span>
X-Pagination-Current-Page: <span class="hljs-number">1</span>
X-Pagination-Per-Page: <span class="hljs-number">20</span>
Link: &lt;http:<span class="hljs-comment">//localhost/users?page=1&gt;; rel=self, </span>
      &lt;http:<span class="hljs-comment">//localhost/users?page=2&gt;; rel=next, </span>
      &lt;http:<span class="hljs-comment">//localhost/users?page=50&gt;; rel=last</span>
Transfer-Encoding: chunked
Content-Type: application/json; charset=UTF-<span class="hljs-number">8</span>

[
    {
        <span class="hljs-string">"id"</span>: <span class="hljs-number">1</span>,
        ...
    },
    {
        <span class="hljs-string">"id"</span>: <span class="hljs-number">2</span>,
        ...
    },
    ...
]
</code></pre>
<p>Try changing the acceptable content type to be <code>application/xml</code>, and you will see the result
is returned in XML format:</p>
<pre><code class="hljs twig"><span class="xml">$ curl -i -H "Accept:application/xml" "http://localhost/users"

HTTP/1.1 200 OK
...
X-Pagination-Total-Count: 1000
X-Pagination-Page-Count: 50
X-Pagination-Current-Page: 1
X-Pagination-Per-Page: 20
Link: <span class="hljs-tag">&lt;<span class="hljs-title">http:</span>//<span class="hljs-attribute">localhost</span>/<span class="hljs-attribute">users</span>?<span class="hljs-attribute">page</span>=<span class="hljs-value">1</span>&gt;</span>; rel=self, 
      <span class="hljs-tag">&lt;<span class="hljs-title">http:</span>//<span class="hljs-attribute">localhost</span>/<span class="hljs-attribute">users</span>?<span class="hljs-attribute">page</span>=<span class="hljs-value">2</span>&gt;</span>; rel=next, 
      <span class="hljs-tag">&lt;<span class="hljs-title">http:</span>//<span class="hljs-attribute">localhost</span>/<span class="hljs-attribute">users</span>?<span class="hljs-attribute">page</span>=<span class="hljs-value">50</span>&gt;</span>; rel=last
Transfer-Encoding: chunked
Content-Type: application/xml

<span class="hljs-pi">&lt;?xml version="1.0" encoding="UTF-8"?&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">response</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-title">item</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-title">id</span>&gt;</span>1<span class="hljs-tag">&lt;/<span class="hljs-title">id</span>&gt;</span>
        ...
    <span class="hljs-tag">&lt;/<span class="hljs-title">item</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-title">item</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-title">id</span>&gt;</span>2<span class="hljs-tag">&lt;/<span class="hljs-title">id</span>&gt;</span>
        ...
    <span class="hljs-tag">&lt;/<span class="hljs-title">item</span>&gt;</span>
    ...
<span class="hljs-tag">&lt;/<span class="hljs-title">response</span>&gt;</span>
</span></code></pre>
<p>The following command will create a new user by sending a POST request with the user data in JSON format:</p>
<pre><code class="hljs bash">$ curl -i -H <span class="hljs-string">"Accept:application/json"</span> -H <span class="hljs-string">"Content-Type:application/json"</span> -XPOST <span class="hljs-string">"http://localhost/users"</span> <span class="hljs-operator">-d</span> <span class="hljs-string">'{"username": "example", "email": "user@example.com"}'</span>

HTTP/<span class="hljs-number">1.1</span> <span class="hljs-number">201</span> Created
...
Location: http://localhost/users/<span class="hljs-number">1</span>
Content-Length: <span class="hljs-number">99</span>
Content-Type: application/json; charset=UTF-<span class="hljs-number">8</span>

{<span class="hljs-string">"id"</span>:<span class="hljs-number">1</span>,<span class="hljs-string">"username"</span>:<span class="hljs-string">"example"</span>,<span class="hljs-string">"email"</span>:<span class="hljs-string">"user@example.com"</span>,<span class="hljs-string">"created_at"</span>:<span class="hljs-number">1414674789</span>,<span class="hljs-string">"updated_at"</span>:<span class="hljs-number">1414674789</span>}
</code></pre>
<blockquote class="tip"><p><strong>Tip: </strong>You may also access your APIs via Web browser by entering the URL <code>http://localhost/users</code>.
  However, you may need some browser plugins to send specific request headers.</p>
</blockquote>
<p>As you can see, in the response headers, there are information about the total count, page count, etc.
There are also links that allow you to navigate to other pages of data. For example, <code>http://localhost/users?page=2</code>
would give you the next page of the user data.</p>
<p>Using the <code>fields</code> and <code>expand</code> parameters, you may also specify which fields should be included in the result.
For example, the URL <code>http://localhost/users?fields=id,email</code> will only return the <code>id</code> and <code>email</code> fields.</p>
<blockquote class="info"><p><strong>Info: </strong>You may have noticed that the result of <code>http://localhost/users</code> includes some sensitive fields,
such as <code>password_hash</code>, <code>auth_key</code>. You certainly do not want these to appear in your API result.
You can and should filter out these fields as described in the <a href="guide-rest-resources.html">Resources</a> section.</p>
</blockquote>
<h2>Summary  <span id="summary"></span><a href="#summary" class="hashlink">&para;</a></h2><p>Using the Yii RESTful API framework, you implement an API endpoint in terms of a controller action, and you use
a controller to organize the actions that implement the endpoints for a single type of resource.</p>
<p>Resources are represented as data models which extend from the <a href="./yii-base-model.html">yii\base\Model</a> class.
If you are working with databases (relational or NoSQL), it is recommended you use <a href="./yii-db-activerecord.html">ActiveRecord</a>
to represent resources.</p>
<p>You may use <a href="./yii-rest-urlrule.html">yii\rest\UrlRule</a> to simplify the routing to your API endpoints.</p>
<p>While not required, it is recommended that you develop your RESTful APIs as a separate application, different from
your Web front end and back end for easier maintenance.</p>
        <div class="toplink"><a href="#" class="h1" title="go to top"><span class="glyphicon glyphicon-arrow-up"></a></div>
    </div>
</div>


</div>

<footer class="footer">
        <p class="pull-right"><small>Page generated on Sat, 09 Jul 2016 12:16:30 +0000</small></p>
    Powered by <a href="http://www.yiiframework.com/" rel="external">Yii Framework</a></footer>

<script type="text/javascript">jQuery(document).ready(function () {
    var shiftWindow = function () { scrollBy(0, -50) };
    if (location.hash) setTimeout(shiftWindow, 1);
    window.addEventListener("hashchange", shiftWindow);
var element = document.createElement("script");
element.src = "./jssearch.index.js";
document.body.appendChild(element);

var searchBox = $('#searchbox');

// search when typing in search field
searchBox.on("keyup", function(event) {
    var query = $(this).val();

    if (query == '' || event.which == 27) {
        $('#search-resultbox').hide();
        return;
    } else if (event.which == 13) {
        var selectedLink = $('#search-resultbox a.selected');
        if (selectedLink.length != 0) {
            document.location = selectedLink.attr('href');
            return;
        }
    } else if (event.which == 38 || event.which == 40) {
        $('#search-resultbox').show();

        var selected = $('#search-resultbox a.selected');
        if (selected.length == 0) {
            $('#search-results').find('a').first().addClass('selected');
        } else {
            var next;
            if (event.which == 40) {
                next = selected.parent().next().find('a').first();
            } else {
                next = selected.parent().prev().find('a').first();
            }
            if (next.length != 0) {
                var resultbox = $('#search-results');
                var position = next.position();

//              TODO scrolling is buggy and jumps around
//                resultbox.scrollTop(Math.floor(position.top));
//                console.log(position.top);

                selected.removeClass('selected');
                next.addClass('selected');
            }
        }

        return;
    }
    $('#search-resultbox').show();
    $('#search-results').html('<li><span class="no-results">No results</span></li>');

    var result = jssearch.search(query);

    if (result.length > 0) {
        var i = 0;
        var resHtml = '';

        for (var key in result) {
            if (i++ > 20) {
                break;
            }
            resHtml = resHtml +
            '<li><a href="' + result[key].file.u.substr(3) +'"><span class="title">' + result[key].file.t + '</span>' +
            '<span class="description">' + result[key].file.d + '</span></a></li>';
        }
        $('#search-results').html(resHtml);
    }
});

// hide the search results on ESC
$(document).on("keyup", function(event) { if (event.which == 27) { $('#search-resultbox').hide(); } });
// hide search results on click to document
$(document).bind('click', function (e) { $('#search-resultbox').hide(); });
// except the following:
searchBox.bind('click', function(e) { e.stopPropagation(); });
$('#search-resultbox').bind('click', function(e) { e.stopPropagation(); });

});</script></body>
</html>
